package com.christina.controller;

import com.christina.entity.Task;
import com.christina.service.TaskService;
import com.christina.utils.Result;
import org.springframework.web.bind.annotation.*;

/**
 * 任务控制器
 * 
 * @author Christina
 */
@RestController
@RequestMapping("/task")
public class TaskController {
    
    private final TaskService taskService;
    
    public TaskController(TaskService taskService) {
        this.taskService = taskService;
        ;
    }
    
    /**
     * 创建任务
     * 
     * @param task 任务对象
     * @return 创建结果
     */
    @PostMapping("/create")
    public Result<Task> createTask(@RequestBody Task task) {
        try {
            Task createdTask = taskService.createTask(task);
            return Result.success("任务创建成功", createdTask);
        } catch (Exception e) {
            return Result.error(500, "任务创建失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取任务详情
     * 
     * @param taskId 任务ID
     * @return 任务详情
     */
    @GetMapping("/get/{taskId}")
    public Result<Task> getTask(@PathVariable Long taskId) {
        try {
            Task task = taskService.getById(taskId);
            if (task != null) {
                return Result.success("获取任务成功", task);
            } else {
                return Result.error(404, "任务不存在");
            }
        } catch (Exception e) {
            return Result.error(500, "获取任务失败: " + e.getMessage());
        }
    }
    
    /**
     * 更新任务状态
     * 
     * @param taskId 任务ID
     * @param status 任务状态
     * @return 更新结果
     */
    @PutMapping("/update-status/{taskId}")
    public Result<String> updateTaskStatus(@PathVariable Long taskId, @RequestParam String status) {
        try {
            boolean updated = taskService.updateTaskStatus(taskId, status);
            if (updated) {
                return Result.success("任务状态更新成功");
            } else {
                return Result.error(404, "任务不存在");
            }
        } catch (Exception e) {
            return Result.error(500, "任务状态更新失败: " + e.getMessage());
        }
    }
    
    /**
     * 删除任务
     * 
     * @param taskId 任务ID
     * @return 删除结果
     */
    @DeleteMapping("/delete/{taskId}")
    public Result<String> deleteTask(@PathVariable Long taskId) {
        try {
            boolean deleted = taskService.deleteTask(taskId);
            if (deleted) {
                return Result.success("任务删除成功");
            } else {
                return Result.error(404, "任务不存在");
            }
        } catch (Exception e) {
            return Result.error(500, "任务删除失败: " + e.getMessage());
        }
    }
}